package org.wmq.travelagency02.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wmq.travelagency02.model.Entity.ChatUser;
import org.wmq.travelagency02.model.Entity.Contact;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.service.ContactService;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("contact")
@RequiredArgsConstructor
public class ContactController {
    private final ContactService contactService;

    @GetMapping("/all/{masterId}")
    public Result<List<ChatUser>> getAll(@PathVariable String masterId){
        List<ChatUser> list = this.contactService.getContactList(masterId);
        if (list == null || list.size() == 0){
            return Result.error("暂无联系人");
        }
        return Result.ok(list);
    }

    @DeleteMapping("/{masterId}/{slaveId}")
    public Result<Void> removeContact(@PathVariable String masterId,@PathVariable String slaveId){
        this.contactService.removeByMasterIdAndSlaveId(masterId,slaveId);
        return Result.ok();
    }

    @PostMapping
    public Result<Void> addContact(@RequestBody Contact contact){
        contact.setCreateTime(LocalDateTime.now());
        contact.setUpdateTime(LocalDateTime.now());
        this.contactService.save(contact);
        return Result.ok();
    }
}
